$(document).ready(function() {
	   
	 $('#searchForm').submit(function(){
		 var s  = "/search/" + $('#songName').val() + "/" + $('#songAuthor').val();
		 window.location = s;
		 return false;
	});
	 changetab(0);
	
 });



function texttofriendlyurl(text2friendlyurl){

	// Clean up the title		
	var url = text2friendlyurl
		.toLowerCase() // change everything to lowercase
		.replace(/^\s+|\s+$/g, "") // trim leading and trailing spaces		
		.replace(/[_|\s]+/g, "-") // change all spaces and underscores to a hyphen
		.replace(/[^a-z0-9-]+/g, "") // remove all non-alphanumeric characters except the hyphen
		.replace(/[-]+/g, "-") // replace multiple instances of the hyphen with a single instance
		.replace(/^-+|-+$/g, "") // trim leading and trailing hyphens				
		;
	
	return url;
	
}

function ssv(){
	var base = "songversion.com";
	var songname = document.getElementById('songname').value;
		
	if(songname == "") window.location = "http://" + base;
	else window.location = "http://" + texttofriendlyurl(songname) + "." + base;
}

function changetab(num){
	
	var tabs = document.getElementById("globalnav").getElementsByTagName("a");

	for (var i = 0, len = tabs.length; i < len; i++ ) {
		if(i==num) 	tabs[ i ].className = 'here';
		else 		tabs[ i ].className = '';
	}	
	
	for(i=0;i<len;i++){
		document.getElementById("tab_" + i).style.display = "none";
	}

	
	
	document.getElementById("tab_" + num).style.display = "";
	if (num != 0)
	{
		loadTabContent('tab_' + num);
	}
}
function loadMoreVersions(){
	var encodedAuthor = $.URLEncode(globalAuthor);
	var encodedSong = $.URLEncode(globalSong);
	var page = 2;
	$.ajax({
		url:'/' + encodedSong + '/' + encodedAuthor + '?tabView=versions&page=' + nextPage,
		success: function(data){
		 	$('#tab_0').append(data);
		 	nextPage++;
	 	}
	 
	 });	
}
function loadTabContent(tabId)
{
	
	var encodedAuthor = $.URLEncode(globalAuthor);
	var encodedSong = $.URLEncode(globalSong);
	var tabView = '';
	if (tabId == 'tab_1') tabView = 'covers';
	if (tabId == 'tab_2') tabView = 'parodies';
	if (tabId == 'tab_3') tabView = 'lyrics';

	$.ajax({
		url:'/' + encodedSong + '/' + encodedAuthor + '?tabView=' + tabView,
		success: function(data){
		 	$('#' + tabId).html(data);
	 	}
	 
	 });	
	
}
